package com.ruoyi.system.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.vo.SysPostVo;
import com.ruoyi.system.domain.bo.SysPostBo;
import com.ruoyi.system.service.ISysPostService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 岗位信息
 *
 * @author ruoyi
 * @date 2023-11-27
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/post")
public class SysPostController extends BaseController {

    private final ISysPostService iSysPostService;

    /**
     * 查询岗位信息列表
     */
    @SaCheckPermission("system:post:list")
    @GetMapping("/list")
    public TableDataInfo<SysPostVo> list(SysPostBo bo, PageQuery pageQuery) {
        return iSysPostService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出岗位信息列表
     */
    @SaCheckPermission("system:post:export")
    @Log(title = "岗位信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysPostBo bo, HttpServletResponse response) {
        List<SysPostVo> list = iSysPostService.queryList(bo);
        ExcelUtil.exportExcel(list, "岗位信息", SysPostVo.class, response);
    }

    /**
     * 获取岗位信息详细信息
     *
     * @param postId 主键
     */
    @SaCheckPermission("system:post:query")
    @GetMapping("/{postId}")
    public R<SysPostVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long postId) {
        return R.ok(iSysPostService.queryById(postId));
    }

    /**
     * 新增岗位信息
     */
    @SaCheckPermission("system:post:add")
    @Log(title = "岗位信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysPostBo bo) {
        return toAjax(iSysPostService.insertByBo(bo));
    }

    /**
     * 修改岗位信息
     */
    @SaCheckPermission("system:post:edit")
    @Log(title = "岗位信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysPostBo bo) {
        return toAjax(iSysPostService.updateByBo(bo));
    }

    /**
     * 删除岗位信息
     *
     * @param postIds 主键串
     */
    @SaCheckPermission("system:post:remove")
    @Log(title = "岗位信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{postIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] postIds) {
        return toAjax(iSysPostService.deleteWithValidByIds(Arrays.asList(postIds), true));
    }
}
